import os
import sys
import time
import json
from flask import request, Flask, Response

app = Flask(__name__)

@app.route("/stream")
def stream():
    key = request.args.get('key')
    print(f"key:{key}")
    def generate():
        for i in range(10):
            res = dict()
            res['idx'] = i
            res['text'] = 'hello world'
            res['timestamp'] = time.time()
            jsn = json.dumps(res)
            time.sleep(1)
            yield f"data:{jsn}\n\n"

    return Response(generate(), mimetype="text/event-stream")